<?php use \Phalcon\Tag as Tag; ?>

<div id="content">
  <div id="content-header">
    <div id="breadcrumb"> 
        <a href="#" class="tip-bottom" data-original-title="Go to Home"><i class="icon-home"></i> 首页</a> 
        <a href="#" class="tip-bottom" data-original-title="">管理</a> 
        <a href="#" class="current">家族</a> 
    </div>
    
  </div>
  <div class="container-fluid">
   <!--  <hr> -->
    <div class="row-fluid">
      <div class="span12">
        <div class="row-fluid">
          <form id="formsearch"  class="form-search">
            <!--家族名称 ：<input name="fname" type="text" class="input-medium search-query" placeholder="" >
            
            <button id="familysearch" type="button" class="btn  btn-success">查&nbsp;询</button>-->
            <button id="btnadd" type="button" class="btn btn-success pull-right">添&nbsp;加</button>
          </form>
            
        </div>
        <div class="widget-box">
          <div class="widget-title"> <span class="icon"> <i class="icon-th"></i> </span>
            <h5>家族列表</h5>
          </div>
          <div class="widget-content nopadding">
            <table id="dataList" class="table table-bordered table-striped">
              <thead>
                <tr>
                  <th>家族名称</th>
                  <th>徽章(家族简称)</th>
                  <th>公告</th>
                  <th>描述</th>
                  <th>家族图标</th>
                  <th>状态</th>
                  <th>操作</th>

                </tr>
              </thead>
              <tbody>
                
              </tbody>
            </table>
          </div>
        </div>

        <div class="row-fluid">
            <div id="pagination" class="pagination alternate pull-right">
              
            </div>
        </div>

        </div>
      </div>
    </div>
  </div>
</div>


<div id="dialog-form" title="Create new user" style=" display: none;">
  <p class="validateTips"></p>
  <div id="userinfoedit">
   <form id="submitform" action="#" method="get" class="form-horizontal">
      <input type="hidden" id="d_uid"> 

      <table >
        <tr>
          <td>
            <div class="control-group">
              <label class="control-label">家族名称:</label>
              <div class="controls">
                <input name="name" type="text" class="span11" placeholder="">
              </div>
            </div>
          </td>
          <td>
            <div class="control-group">
              <label class="control-label">徽章:</label>
              <div class="controls">
                <input name="shortName" type="text" class="span11">
                <span class="help-block"></span>
              </div>
            </div>
          </td>
        </tr>

        <tr>
          <td>
            <div class="control-group">
              <label class="control-label">公告:</label>
              <div class="controls">
                <input name="announcement" type="text" class="span11" placeholder="">
              </div>
            </div>
          </td>
          <td>
            <div class="control-group">
              <label class="control-label">描述 :</label>
              <div class="controls">
                <input name="description" type="text" class="span11">
                <span class="help-block"></span>
              </div>
            </div>
          </td>
        </tr>

        <tr>
        <td>
            <div class="control-group">
              <label class="control-label">家族图标 :</label>
              <div class="controls">

                <input name="logo" type="text" class="span11" >
               
              </div>
            </div>
          </td>
          <td>
              <div class="control-group">
                  <label class="control-label">状态 :</label>
                  <div class="controls">
                      <select name="status" class="span11">
                          <option value="0">未审批</option>
                          <option value="1">正常</option>
                      </select>

                      <span class="help-block"></span>
                  </div>
              </div>
          </td>
        </tr>

      </table>  
      
    </form>
  </div>
</div>

<script type="text/javascript">

  /*var search=document.getElementById('familysearch');

  utils.addEvent(search,'click',searchCallback);

  function searchCallback(){
    var formsearch=document.getElementById('formsearch');
   
    data={};

    data.fname=formsearch.fname.value;
    
    $.ajax({
          type:"post",
          data:data,
          url:"/formalmgr/search",
          dataType:'json',
          success:function(res){
            var data= res.data;
            var table=document.getElementById("familyteable");
            removeAllRow(table);
            for(var i=0;i<data.length;i++){
              addRow(table,'',data[i]);
            }
            
          }
    });

  }*/

$(function(){
    getDataList()
});

/**
* 初始化为空
*/
function Initialization(){

  var submitform=document.getElementById('submitform');
    
    submitform.name.value='';
    submitform.shortName.value='';
    submitform.announcement.value='';
    submitform.description.value='';
    submitform.logo.value='';
    submitform.status.value='';
}

var btnadd=document.getElementById('btnadd');
utils.addEvent(btnadd,'click',addData);

function addData(){
  $( "#dialog-form" ).dialog({
      width: 700,
      title:'签约主播',
      modal:true,
      closeOnEscape:true,
      resizable:false,
      open:function(res){
          Initialization();
        },
      buttons:{
        "添加":function(){

          var submitform=document.getElementById('submitform');
          
          data={};

          data.action="add"
          data.name=submitform.name.value;
          data.shortName=submitform.shortName.value;
          data.announcement=submitform.announcement.value;
          data.description= submitform.description.value;
          data.logo= submitform.logo.value;
          data.status= submitform.status.value;

          $.ajax({
              type:"post",
              url:"/familymgr/updateFamily",
              data:data,
              dataType:'json',
              success:function(res){
                if(res.code==0){
                  getDataList();
                  $("#dialog-form" ).dialog("close");
                }else{
                  alert(res.info);
                }
              }
          });
        },
        "取消":function(){
          $(this).dialog("close");
        }

      }
  });
 }

function getDataList(index){
  var data={};
  data.page=index||0;
  data.numPerPage=pagesize;
  $.ajax({
    type:"post",
    url:"/familymgr/getFamily",
    data:data,
    dataType:'json',   
    success:function(res){
      
      var data=res.data;
      var table=document.getElementById("dataList");
      var tbody= table.getElementsByTagName('tbody')[0];

      dataTable.clearData(tbody);
      
      for(var i=0;i<data.list.length;i++){
        addRow(tbody,'',data.list[i]);
      }
      
      if(!index||index==0){
        initPagination(data.count);
      }

    }
  });
}

function initPagination(page) {
  $("#pagination").pagination(page, {
      callback: pageselectCallback,
      items_per_page:pagesize, // 每页只显示一条记录
      num_display_entries : 3,
      num_edge_entries : 1, 
      prev_text : "上一页",  
      next_text : "下一页", 
  });
}

function pageselectCallback(page_index, jq){
    getDataList(page_index);
    return false;
}

function addRow(table,index,data){
  var row=table.insertRow();
  var cell=row.insertCell(0);
  var cell1=row.insertCell(1);
  var cell2=row.insertCell(2);
  var cell3=row.insertCell(3);
  var cell4=row.insertCell(4);
  var cell5=row.insertCell(5);
  var cell6=row.insertCell(6);

  cell.innerHTML=data.name;
  cell1.innerHTML=data.shortName;
  cell2.innerHTML=data.announcement;
  cell3.innerHTML=data.description;
  cell4.innerHTML=data.logo;
  cell5.innerHTML=data.status==1?"正常":"未审批";

  var deit=document.createElement("a"),
  del=document.createElement("a");
  
  deit.innerHTML='编辑';
  deit.className="table-edit";
  deit.data=data.id;
  utils.addEvent(deit,'click',dataEdit);

  del.innerHTML='删除';
  del.data=data.id; 
  utils.addEvent(del,'click',dataDelete); 

  cell6.appendChild(deit);
  cell6.appendChild(del);
    
  return row;
}

function dataEdit(){
  var btn=this;  
  var url="/familymgr/updateFamily/"+btn.data;
  
   $( "#dialog-form" ).dialog({
        width: 700,
        title:'修改家族信息',
        modal:true,
        closeOnEscape:true,
        resizable:false,
        open:function(){
          var data={};
          data.action='';

          Initialization();

          $.ajax({
            type:"post",
            data:data,
            url:url,
            dataType:'json',
            success:function(res){
              var data= res.data;

              var submitform=document.getElementById('submitform');

              submitform.name.value=data.name;
              submitform.shortName.value=data.shortName;
              submitform.announcement.value=data.announcement;
              submitform.description.value=data.description;
              submitform.logo.value=data.logo;
              submitform.status.value=data.status;
            }
          });
        },
        buttons:{
          "修改":function(){

            var submitform=document.getElementById('submitform');

            data={};
            data.action="update"; 

            data.name=submitform.name.value;
            data.shortName=submitform.shortName.value;
            data.announcement=submitform.announcement.value;
            data.description= submitform.description.value;
            data.logo= submitform.logo.value;
            data.status= submitform.status.value;

            $.ajax({
              type:"post",
              data:data,
              url:url,
              dataType:'json',
              success:function(res){
                if(res.code==0){
                  getDataList();
                  $("#dialog-form" ).dialog("close");
                }else{
                  alert(res.info);
                }       
              }
            });

          },
          "取消":function(){
            $(this).dialog("close");
          }

        }
   });

}

function dataDelete(){
  if(confirm("确定要删除该数据")){
    var btn=this;  
    var url="/familymgr/updateFamily/"+btn.data,
    data={};
    data.action="del";
    $.ajax({
      type:"post",
      data:data,
      url:url,
      dataType:'json',
      success:function(res){
        if(res.code==0){
          getDataList();
         //dataTable.delTableRow(btn);
        }else{
          alert(res.info);
        }      
      }
    });
  }
}





</script>

